package com.hzhc.controller.wxapp;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.common.BaseController;
import com.hzhc.dto.UserDto;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.CntrInfoService;
import com.hzhc.service.TrainPlanDetailService;
import com.hzhc.service.TrainPlanService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.CntrTallyVo;
import com.hzhc.vo.TrainPlanDetailVo;
import com.hzhc.vo.TrainPlanVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


/**
 * @author wuxiaoke
 * @description app 卸车理货控制器
 * @date 2024/6/5 上午9:41
 */

@RestController
@RequiredArgsConstructor
@RequestMapping("/app")
public class WxApiCntrXcTallyController extends BaseController {
    private final TrainPlanService trainPlanService;
    private final TrainPlanDetailService trainPlanDetailService;
    private final CntrInfoService cntrInfoService;

    /**
     * 查询app端 班列接口 (ZC+XC)
     */
    @RequestMapping("/getAppTrainPlanList")
    public ResultUtil<List<TrainPlanVo>> getGateCarList(@RequestBody TrainPlanVo vo) {
        Page<TrainPlanVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        vo.setLoadZl(1);//已下发
        List<TrainPlanVo> trainPlanVos = new ArrayList<>();
        if ("ZC".equals(vo.getType())){
            trainPlanVos = trainPlanService.getAppTrainPlanList(page, vo);
        }else if("XC".equals(vo.getType())){
            trainPlanVos = trainPlanService.getAppXcTrainPlanList(page, vo);
        }
        return ResultUtil.build(trainPlanVos,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 查询班列底下的箱子数据 (不用)
     */
    @RequestMapping("/getTrainPlanDetailList")
    public ResultUtil<List<TrainPlanDetailVo>> getGateCarList(@RequestBody TrainPlanDetailVo vo) {
        Page<TrainPlanDetailVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<TrainPlanDetailVo> trainPlanDetailVos = trainPlanDetailService.getAllTrainPlanDetail(page, vo);
        return ResultUtil.build(trainPlanDetailVos,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    //app 卸车理货列表
    @RequestMapping("/getAllXcTrainPlanList")
    public ResultUtil<List<TrainPlanDetailVo>> getAllXcTrainPlanList(HttpServletRequest request,@RequestBody TrainPlanDetailVo vo){
        Page<TrainPlanDetailVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<TrainPlanDetailVo> trainPlanDetailVos = new ArrayList<>();
        if (vo.getIsZzt()==0){
            trainPlanDetailVos = trainPlanDetailService.getXcTrainPlanDetail(page, vo);//(非直提)
        }else{
            trainPlanDetailVos = trainPlanDetailService.getZtTrainPlanDetail(page, vo);//直提
        }
        return ResultUtil.build(trainPlanDetailVos,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 卸车理货
     */
    @PostMapping("/cntrXcTally")
    public ResultUtil<T> cntrXcTally(HttpServletRequest request, @RequestBody CntrTallyVo vo) {
        UserDto userMember = TokenUtil.getUserMember(request);
        vo.setMid(userMember.getId());
        vo.setUserName(userMember.getRealname());
        vo.setAccesspath("APP端");
        if (vo.getIsZzt()==0){
            return cntrInfoService.cntrXcTally(vo);//普通卸车
        }
        return cntrInfoService.cntrXcTallyZt(vo);//直提卸车
    }

}
